import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'
import { HiddenControlSchema } from 'amis/lib/renderers/Form/Hidden'
import { TextControlSchema } from 'amis/lib/renderers/Form/InputText'
import { DateTimeControlSchema } from 'amis/lib/renderers/Form/InputDate'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import { DefaultCrudTableServerApi } from '@/components/DefaultCrudTable/types/DefaultCrudTableProps'
import iamAppIdSelectUtils from '@/components/account/iamAppIdSelectUtils'
import { SelectControlSchema } from 'amis/lib/renderers/Form/Select'
import iamAppIdColumnUtils from '@/components/account/iamAppIdColumnUtils'
import iamAccountProfileSexColumnUtils from '@/components/account/iamAccountProfileSexColumnUtils'
import iamAccountProfileSexSelectUtils from '@/components/account/iamAccountProfileSexSelectUtils'
import iamAccountProfileRegisterSourceFromColumnUtils from '@/components/account/iamAccountProfileRegisterSourceFromColumnUtils'
import iamAccountProfileRegisterSourceFromSelectUtils from '@/components/account/iamAccountProfileRegisterSourceFromSelectUtils'
import iamAccountIdForProfileSelectUtils from '@/components/account/iamAccountIdForProfileSelectUtils'
import { DateRangeControlSchema } from 'amis/lib/renderers/Form/InputDateRange'
import iamAccountIdSelectUtils from '@/components/account/iamAccountIdSelectUtils'
import { TextareaControlSchema } from 'amis/lib/renderers/Form/Textarea'
import { ImageControlSchema } from 'amis/lib/renderers/Form/InputImage'
import accountAdminApiConfig from '@/core/account/generated/apiconfig/accountAdminApiConfig'

const IamAccountProfileList = () => {
	const entityName = '账户基础资料'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = accountAdminApiConfig.iamAccountProfile
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'iamAppId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'accountId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'nickName',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'realName',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'sex',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'registerSourceFrom',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.GREATER_OR_EQUAL,
						propertyName: 'birthday',
						paramPropertyName: 'birthdayBegin',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.LESS_OR_EQUAL,
						propertyName: 'birthday',
						paramPropertyName: 'birthdayEnd',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: { apiConfig: modelApiConfig.add },
		deleteServerApi: {
			apiConfig: modelApiConfig.removeLogic,
		},
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
		},
		editServerApi: {
			apiConfig: modelApiConfig.modifyNotNullField,
			data: {
				id: '${id}',
				nickName: '${nickName}',
				realName: '${realName}',
				avatar: '${avatar}',
				signature: '${signature}',
				sex: '${sex}',
				registerSourceFrom: '${registerSourceFrom}',
				birthday: '${birthday}',
			},
		} as DefaultCrudTableServerApi,
		debug: false,
		formItems: [
			{
				type: 'hidden',
				name: 'id',
				label: 'ID',
			} as HiddenControlSchema,
			{
				...iamAppIdSelectUtils.getSchema(),
				required: true,
				disabledOn: isModifyModeExpression,
			} as SelectControlSchema,
			{
				...iamAccountIdSelectUtils.getSchema({ name: 'accountId' }),
				required: true,
				disabledOn: isModifyModeExpression,
			} as SelectControlSchema,
			{
				type: 'input-text',
				name: 'nickName',
				label: '昵称',
				required: true,
				maxLength: 36,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'realName',
				label: '真实姓名',
				required: true,
				maxLength: 36,
			} as TextControlSchema,
			{
				type: 'input-image',
				name: 'avatar',
				label: '头像',
				autoUpload: true,
				receiver: '/amis/api/upload/file',
			} as ImageControlSchema,
			{
				...iamAccountProfileRegisterSourceFromSelectUtils.getSchema({ name: 'registerSourceFrom' }),
				required: true,
			} as SelectControlSchema,
			{
				type: 'input-datetime',
				name: 'birthday',
				label: '生日时间',
				valueFormat: 'x',
				displayFormat: 'YYYY-MM-DD HH:mm:ss',
				required: false,
			} as DateTimeControlSchema,
			{
				type: 'textarea',
				name: 'signature',
				label: '个性签名',
				required: false,
				maxLength: 250,
				showCounter: true,
			} as TextareaControlSchema,
			{
				...iamAccountProfileSexSelectUtils.getSchema(),
				required: true,
			} as SelectControlSchema,
			{
				type: 'input-datetime',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
			{
				type: 'input-datetime',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
		],
		columns: [
			{
				name: 'id',
				label: 'ID',
				searchable: true,
			},
			{
				...iamAppIdColumnUtils.getSchema(),
				searchable: iamAppIdSelectUtils.getSchema(),
			},
			{
				name: 'accountId',
				label: '账户ID',
				searchable: iamAccountIdForProfileSelectUtils.getSchema({ name: 'accountId' }),
			},
			{
				name: 'nickName',
				label: '昵称',
				searchable: true,
			},
			{
				name: 'realName',
				label: '姓名',
				searchable: true,
			},
			{
				name: 'avatar',
				label: '头像',
			},
			{
				name: 'signature',
				label: '个性签名',
			},
			{
				...iamAccountProfileSexColumnUtils.getSchema(),
				searchable: iamAccountProfileSexSelectUtils.getSchema(),
			},
			{
				...iamAccountProfileRegisterSourceFromColumnUtils.getSchema(),
				searchable: iamAccountProfileRegisterSourceFromSelectUtils.getSchema(),
			},
			{
				type: 'date',
				name: 'birthday',
				label: '生日时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				searchable: {
					type: 'input-datetime-range',
					name: 'birthdayBegin',
					extraName: 'birthdayEnd',
					label: '生日时间',
					valueFormat: 'x',
					displayFormat: 'YYYY-MM-DD HH:mm:ss',
					shortcuts: 'today,yesterday,7daysago,prevweek,thismonth,prevmonth,thisyear,lastyear',
					timeFormat: 'HH:mm:ss',
				} as DateRangeControlSchema,
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default IamAccountProfileList
